package com.ciquan.server.util;

import com.ciquan.server.annotation.ExcelColumn;
import com.ciquan.server.bean.BusinessStatus;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by kakalee on 16/3/4.
 */
public class ExcelUtils {

    public static HSSFSheet readSheet(InputStream inputStream, int sheetIndex) throws IOException{
        POIFSFileSystem fs =new POIFSFileSystem(inputStream);
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        return wb.getSheetAt(sheetIndex);
    }

    public static HSSFRow readRow(HSSFSheet sheet, int r) {
        return sheet.getRow(r);
    }

    public static HSSFCell readCell(HSSFSheet sheet, int r, int c) {
        HSSFRow row = sheet.getRow(r);
        return row.getCell(c);
    }

    public static <T> List<T> parse(InputStream inputStream, Class<T> clazz) throws Exception{
        List<T> result = new ArrayList<T>();
        HSSFSheet sheet = readSheet(inputStream, 0);

        int begin = sheet.getFirstRowNum();
        HSSFRow row = sheet.getRow(begin);

        Field[] fields = clazz.getDeclaredFields();
        Map<Integer, Field> fieldMap = new HashMap<Integer, Field>();
        for(Field field : fields) {
            if(field.isAnnotationPresent(ExcelColumn.class)) {
                String columnName = field.getAnnotation(ExcelColumn.class).value();
                for(int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
                    HSSFCell cell = row.getCell(i);
                    String cellName = cell.toString().replaceAll("   ", "");
                    if(columnName.equals(cellName)) {
                        fieldMap.put(i, field);
                    }
                }
            }
        }

        int size = sheet.getLastRowNum();
        for(int i = begin + 1; i <= size; i++) {
            row = sheet.getRow(i);
            T bean = clazz.newInstance();
            for(int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
                HSSFCell cell = row.getCell(j);
                Field field = fieldMap.get(j);
                if(field != null) {
                    Class parser = field.getAnnotation(ExcelColumn.class).valuePaser();
                    ValueParser valueParser = (ValueParser) parser.newInstance();
                    field.setAccessible(true);
                    field.set(bean, valueParser.parse(cell.toString()));
                }
            }
            result.add(bean);
        }
        return result;
    }


    public static void main(String args[]) throws Exception{
        List<BusinessStatus> businessStatuses = parse(new FileInputStream("/Users/kakalee/Downloads/201408经营概况PC端.xls"), BusinessStatus.class);
    }
}
